﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class user_productDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadProductByID();
            LoadValueRating();
            LoadTotalComment();
            if (RatingProduct.CurrentRating == 0)
                lblRating.Text = "Not yet rated";
            if (Session["login"] == null)
            {
                FormComment1.Visible = false;
            }

        }
    }
    public void LoadProductByID()
    {
        int idProduct = Convert.ToInt32(Request.QueryString["ID"]);
        ProductBus productBus = new ProductBus();
        DataTable dt = new DataTable();
        dt = productBus.GetProductByID(idProduct);
        //bind data cho detailview
        DetailsViewProduct.DataSource = dt;
        DetailsViewProduct.DataBind();

        //bind data
        DetailsViewID.DataSource = dt;
        DetailsViewID.DataBind();

        //tang so lan view cho san pham
        int visit = Convert.ToInt32(productBus.GetVisitProduct(idProduct).Rows[0]["pd_visit"]);
        int newvisit = visit + 1;
        productBus.UpdateVisitProduct(idProduct, newvisit);
    }
    protected void ImagebtnAddtocard_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "AddtoCard")
        {
            int idProduct = Convert.ToInt32(e.CommandArgument);
            ShoppingCart.Instance.AddItem(idProduct);
            //tang so luot mua            
            ProductBus productBus = new ProductBus();
            int currentOrdernum = (int)productBus.GetOrderNum(idProduct).Rows[0]["pd_ordernum"];
            int newOrdernum = currentOrdernum + 1;
            productBus.UpdateOrderNum(idProduct, newOrdernum);
            //chuyen tiep den trang hien thi gio hang
            Response.Redirect("shoppingcart.aspx");
        }
    }

    protected void RatingProduct_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        int value = int.Parse(e.Value);
        string result = string.Empty;
        switch (value)
        {
            case 0:
                result = "Not yet rated";
                break;
            case 1:
                result = "Bad!";
                break;
            case 2:
                result = "Not very good";
                break;
            case 3:
                result = "Average";
                break;
            case 4:
                result = "Good";
                break;
            case 5:
                result = "Very good";
                break;

        }
        lblRating.Text = result;
        if (Session["login"] != null)
        {
            // kich hoat thuoc tich readonly
            RatingProduct.ReadOnly = true;
            RatingBus ratingBus = new RatingBus();
            Rating objRating = new Rating();
            int idProduct = Convert.ToInt32(Request.QueryString["ID"]);

            // lay ID cua nguoi su dung
            String email = Session["email"].ToString();
            String pass = Session["password"].ToString();
            CustomerBus customerBus = new CustomerBus();
            int cusID = (int)customerBus.getInfoCustomer(email, pass).Rows[0]["cus_id"];

            //set gia tri cho doi tuong va insert
            objRating.PdID = idProduct;
            objRating.CusID = cusID;
            objRating.RatingPoint = value;
            ratingBus.InsertRating(objRating);
        }
        else if (Session["login"] == null)
        {
            Label1.Text = "You are not login ! Please login for rating";
            Label1.ForeColor = System.Drawing.Color.Red;
        }
    }

    public void LoadValueRating()
    {
        // lay ID cua nguoi su dung
        if (Session["login"] != null)
        {
            String email = Session["email"].ToString();
            String pass = Session["password"].ToString();

            CustomerBus customerBus = new CustomerBus();
            int cusID = (int)customerBus.getInfoCustomer(email, pass).Rows[0]["cus_id"];
            // lay ID product
            int idProduct = Convert.ToInt32(Request.QueryString["ID"]);
            RatingBus ratingBus = new RatingBus();

            if ((int)ratingBus.GetRatingByID(idProduct, cusID).Rows.Count > 0)
            {
                //neu nguoi dung binh chon san pham nay roi thi ko cho binh chon tiep
                RatingProduct.ReadOnly = true;
                RatingProduct.CurrentRating = (int)ratingBus.GetRatingByID(idProduct, cusID).Rows[0]["rating"];
                lblRating.Text = "Your rating(" + ratingBus.GetRatingByID(idProduct, cusID).Rows[0]["rating"].ToString() + ")";
                lblRating.ForeColor = System.Drawing.Color.YellowGreen;
                Label1.Text = "You have rating this product !";
                Label1.ForeColor = System.Drawing.Color.Red;

            }
        }

        // lay ID product
        int idProduct2 = Convert.ToInt32(Request.QueryString["ID"]);
        RatingDAL ratingDAL = new RatingDAL();
        LabelTotal.Text = "Hit(" + ratingDAL.GetTotalRatingByPDID(idProduct2).Rows.Count.ToString() + ")";
        LabelSumOfTotal.Text = "Star(" + ratingDAL.GetSumPointByPDID(idProduct2).Rows[0]["sumPoint"].ToString() + ")";

    }
    public void LoadTotalComment()
    {
        int idProduct = Convert.ToInt32(Request.QueryString["ID"]);
        CommentBus commentBus = new CommentBus();
        String total = commentBus.GetCommentByPDID(idProduct).Rows.Count.ToString();
        LabelTotalComment.Text = total;
    }
}
